package com.main;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Main extends JFrame{
	private int width = 600;
	private int height = 600;
	
	private Point maze[][] = new Point[100][100];
	//private Graphics g;
	private JPanel pane;
	
	private int w=75,h=55;
	private int offsetX = 20,offsetY = 20;
	
	private int jointy[] = new int[1000];//关节点x
	private int jointx[] = new int[1000];//关节点y
	
	private int beginx=380,beginy=30;
	private int endx=380,endy=580;
	int ex = endx;
	int ey = 550;
	int bx = 300;
	int by = beginy;
	
	int lastr;
	
	int s1,s2;
	double s;
	
	int xlength = 0,ylength = 0,xlength2 = 9;
	
	int index=-1,indexy=-1;
	
	int hx[] = new int[1000];
	int hy[] = new int[1000];
	
	final double _C = 100.0;//100
	final int _R = 90;//90
	final int _OR = 10;//10
	
	int allLeft;
	int allRight = 300;
	public Main() {
		
		super("2d迷宫");
		pane = new JPanel();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int screenWidth = (int)screenSize.getWidth();
		int screenHeight = (int)screenSize.getHeight();
		
		setSize(width,height);
		setLocation((screenWidth-width)/2,(screenHeight-height)/2);
		
		
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(pane);
		
		this.createMaze();
		this.createJointData();
		this.remakeJoint();
	}
	public void createMaze() {
		
		for(int i = 0;i < h; i++) {
			for(int j = 0; j < w; j++) {
				Random r = new Random();
				boolean x = r.nextBoolean();
				boolean y = r.nextBoolean();
				Point point = new Point(x,y);
				maze[i][j] = point;
			}
		}
		
		//System.out.println(maze);
	}
	public void createJointData() {
		
		do{
			Random r = new Random();
			int direction = r.nextInt(3)+1;
			
			
			switch(direction){
				//右
				case 1:{
					if(xlength<width) {
						Random r1 = new Random();
						int a = r1.nextInt(_R) + _OR;
						
						index++;
						xlength += a;
						
						jointx[index] += xlength;
						ex = jointx[index];
						
						allRight +=a;
						
						lastr = direction;
						break;
					}
					
				}
				//左
				case 3:{
					if(xlength2>=0) {
					
						Random r3 = new Random();
						int a = r3.nextInt(_R) + _OR;
						index++;
						xlength2 -= a;
						jointx[index] -= xlength2;
						ex = jointx[index];
						
						allLeft-=a;
						
						lastr = direction;
						
					}
					break;
				}
			
				//下
				case 2:{
//					if(ylength<height+200) {
						Random r2 = new Random();
						int a = r2.nextInt(_R) + _OR;
						
						indexy++;
						ylength +=a;
						
						jointy[indexy] += ylength;
						
						by = jointy[indexy];
						
						lastr = direction;
//					}
					break;
				}
			}
			s1 = (int)Math.pow((double)(300-(allRight-allLeft)), 2.0);
			s2 = (int)Math.pow((double)(ey-by), 2.0);
			s = Math.sqrt((double)(s1+s2));
			
		}while(s > _C);
		

		
		
		
	}
	public void remakeJoint() {
		System.out.println(1111);
//		Graphics g = pane.getGraphics();
		//g.drawRect(10, 10, 100, 100);
		System.arraycopy(jointx, 0, hx, 0, 20);
		System.arraycopy(jointy, 0, hy, 0, 20);
		for(int i =0;i<100;i++) {
//			System.out.println("x点数据为："+hx[i]+",y点数据为："+hy[i]);
			hx[i*2] = hx[i];
			hy[i*2] = hy[i];
//			System.out.println("x点数据为："+hx[i]+",y点数据为："+hy[i]);
		}
		for(int i =1;i<100;i+=2) {
			hx[i] = hx[i+1];
			hy[i] = hy[i-1];
			
		}
		for(int i = 0;i<100;i++) {
			System.out.println("x点数据为："+jointx[i]+",y点数据为："+jointy[i]);
//			System.out.println("x点数据为："+hx[i]+",y点数据为："+hy[i]);
			
		}
//		Graphics g = pane.getGraphics();
//		for(int i = 0;i<20;i ++) {
//		
////			System.out.println("x点数据为："+jointx[i]+",y点数据为："+jointy[i]);
//			
//			g.setColor(Color.red);
//			g.drawLine(hx[i],hy[i],hx[i+1],hy[i+1]);
//		
//		}
	}
	public void paint(Graphics g) {
//		this.remakeJoint();
//		//g.drawRect(0, 0, 200, 200);
//		
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(maze[i][j].y) {
					//System.out.println(6);
					g.drawLine(offsetX+j*10, offsetY+i*10, offsetX+j*10, offsetY+i*10+10);
				}
				if(maze[i][j].x) {
					g.drawLine(offsetX+j*10, offsetY+i*10, offsetX+j*10+10, offsetY+i*10);
				}
			}
		}
//		
//		for(int i = 0;i<indexy ;i ++) {
//			
////			System.out.println("x点数据为："+jointx[i]+",y点数据为："+jointy[i]);
//			
//			g.setColor(Color.red);
//			g.drawLine(jointx[i],jointy[i],jointx[i+1],jointy[i+1]);
//			
//		}
		
		for(int i = 0;i<100 ;i ++) {
			
			//System.out.println("x点数据为："+jointx[i]+",y点数据为："+jointy[i]);
			
			g.setColor(Color.red);
			g.drawLine(hx[i],hy[i],hx[i+1],hy[i+1]);
			g.drawLine(jointx[i],jointy[i],jointx[i+1],jointy[i+1]);
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main();
	}

}
class Point{
	public boolean x;
	public boolean y;
	public Point() {
		
	}
	public Point(boolean x,boolean y) {
		this.x = x;
		this.y = y;
	}
}